package com.czkt.springboot_mybatis_test.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.czkt.springboot_mybatis_test.entity.Page;
import com.czkt.springboot_mybatis_test.entity.SysRole;
import com.czkt.springboot_mybatis_test.service.ISysRoleService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2024-06-18
 */
@Controller
@RequestMapping("/springboot_mybatis_test/sys-role")
public class SysRoleController {
    @Resource
    ISysRoleService roleService;

    @RequestMapping("/crm/role/list")
    public String roleList(String roleName,Integer pageIndex, Model model){
        if(pageIndex == null){
            pageIndex = 1;
        }
        Page page = new Page();
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        if(roleName != null && !roleName.equals("")){
            queryWrapper.eq("role_name",roleName);
        }
        int count = roleService.count(queryWrapper);
        if(count == 0){
            count = 1;
        }
        page.setTotalCount(count);
        if(pageIndex < 1){
            pageIndex = 1;
        }
        if(pageIndex > page.getTotalPageCount()){
            pageIndex = page.getTotalPageCount();
        }
        int index = (pageIndex - 1) * page.getPageSize();
        page.setPageNo(pageIndex);
        if(roleName == null){
            roleName = "";
        }
        List<SysRole> role = roleService.roleList(roleName,index,page.getPageSize());
        page.setContent(role);
        model.addAttribute("rolePage",page);
        model.addAttribute("roleName",roleName);
        return "/role/list";
    }
}
